package com.alex.space.commons.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author Alex
 * Created by Alex on 2018/2/5.
 */
public class ThreadTest {

    public static void main(String[] args) {

        String[] idList = new String[1000];
        for (int i = 1; i < 1000; i++) {
            idList[i - 1] = String.valueOf(i);
        }

        try {

            int nThread = Runtime.getRuntime().availableProcessors() * 2;

            ExecutorService pool = Executors.newFixedThreadPool(nThread);

            List<Future<List>> list = new ArrayList<Future<List>>();

            for (String id : idList) {
                Callable c = new TagsCallable(id);
                Future f = pool.submit(c);
                list.add(f);
            }

            System.out.println("shutdown thread pool");
            // 关闭线程池
            pool.shutdown();

            int count = 0;
            for (Future<List> future : list) {
                System.out.println("process thread ");
                try {
                    List tempList = future.get();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                count++;
            }

            System.out.println("count:" + count);


        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("finish");

    }
}
